<?php
/**
 * Created by PhpStorm.
 * User: Rag_Panda
 * Date: 2015/12/3
 * Time: 15:52
 */

namespace BaseClass\Component\Article;
use Illuminate\Support\Facades\DB;


/**
 * Class ArticleSubject
 * @package BaseClass\Component\Article
 */
class ArticleSubject
{
    /**
     * @var
     */
    public $subject_id;
    /**
     * @var bool
     */
    public $subject_info;
    /**
     * @var bool
     */
    public $article_info;
    static function select($query_limit)
    {
        /*
         * $query_limit
         * |-user   按照用户筛选
         * |-sort   排序方式
         * |-num    每页条数
         * |-start  起始条数
         * |-desc 是否倒序列
         * |-paginate 分页 制定分页数
         *
         *
         * $return_data
         * |-status 是否成功
         * |-message 消息
         * |-data   数据 DB返回的二维结构
         *
         */
        $query = DB::table("base_article_subject");
        //排序
        if(isset($query_limit['sort']))
        {
            //有了排序方式之后再判断是否倒序
            if(isset($query_limit['desc']) && $query_limit['desc']==true)
            {
                $query = $query->orderBy($query_limit['sort'],"desc");
            }
            else
            {
                $query = $query->orderBy($query_limit['sort']);
            }
        }
        else
        {
            //没有排序方式就默认按照专题ID降序排序
            $query = $query->orderBy("subject_id","desc");
        }
        //按照用户查找
        if(isset($query_limit["user"]))
        {
            $query = $query->where("article_user","=",$query_limit["user"]);
        }


        //分页
        //计算出总条数
        $num_query = clone $query;
        $return_data["total"] = $num_query->select(DB::raw('count(*) as num'))->first()->num;
        //起始条数
        if(isset($query_limit['start']))
        {
            $query = $query->skip($query_limit['start']);
        }
        //每页条数
        if(isset($query_limit['num']))
        {
            if($query_limit["num"]==0)//如果指定0，直接就不用返回数据了
            {
                $return_data["status"] = true;
                $return_data["message"] = "查看到数据，但数量限制为0";
                $return_data["data"] = [];
                return $return_data;
            }
            $query = $query->take($query_limit['num']);
        }
        //如果没有指定条数就默认给一个
        else
        {
            $query = $query->take(config("my_config.default_num_page"));
        }

        $query = $query->leftJoin("base_user","user_id","=","subject_user");

        if(isset($query_limit["paginate"]))
        {
            $data = $query->simplePaginate($query_limit["paginate"]);
        }
        else
        {
            $data = $query->get();
        }

        //返回数据
        $return_data["status"] = true;
        $return_data["message"] = "成功获取到数据";
        $return_data["data"] =  $data;
        return $return_data;
    }

    /**
     * @param $user_id
     * @return bool
     */
    static function getMoreByUser($user_id)
    {
       $result=DB::table("base_article_subject")->where("subject_user","=",$user_id)->simplePaginate(10);
        if($result)
        {
            return $result;
        }
        else
        {
            return false;
        }

    }

    /**
     * @param $info_array
     * @return bool
     */
    static function add($info_array)
    {
       if(!isset($info_array['subject_user']))
       {
            $info_array['subject_user']=session("user.user_id");
       }
        $info_array['subject_create_date']=date('Y-m-d H:i:s',time());
        $info_array['subject_update_date']=date('Y-m-d H:i:s',time());
        if(DB::table("base_article_subject")->insert($info_array))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * @param $subject_id
     */
    public function __construct($subject_id)
    {
        $this->subject_id=$subject_id;
        $this->syncBaseInfo();
        $this->syncArticleInfo();

    }

    /**
     * @return bool
     */
    public function syncBaseInfo()
    {
        $subject_info=DB::table("base_article_subject")->where("subject_id","=",$this->subject_id)->first();
        if($subject_info)
        {
            $this->subject_info = $subject_info;
            DB::table("base_article_subject")->where("subject_id","=",$this->subject_info->subject_id++)->increment('subject_click');
            return true;

        }
        else
        {
            return false;
        }
    }

    /**
     * @return bool
     */
    public function syncArticleInfo()
    {
        $article_info=DB::table("base_article_re_subject")
            ->join("base_article","relation_article","=","article_id")
            ->join("base_article_subject","relation_subject","=","subject_id")
            ->where("subject_id","=",$this->subject_id)
            ->get();
        if($article_info!=NULL)
        {
            $this->article_info=$article_info;
            return $article_info;
        }
        else
        {
            $this->article_info = [];
            return false;
        }
    }

    /**
     * @param $data
     * @return bool
     */
    public function SubjectUpdate($data)
    {
        $data["subject_update_date"]=date('Y-m-d H:i:s',time());
        if(DB::table("base_article_subject")->where('subject_id',"=","$this->subject_id")->update($data))
        {
            $this->syncBaseInfo();
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * @return bool
     */
    public function SubjectDelete()
    {
        if(DB::table("base_article_subject")->where('subject_id',"=","$this->subject_id")->delete())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * @param $relation_article
     * @return bool
     */
    public function SubjectAddArticle($relation_article)
    {
        $data['relation_article']=$relation_article;
        $data['relation_subject']=$this->subject_id;
        $date['relation_sort']=0;
        if(DB::table("base_article_re_subject")->insert($data))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * @param $relation_article
     * @return bool
     */
    public function SubjectRemoveArticle($relation_article)
    {
        if(DB::table("base_article_re_subject")->where("relation_article","=",$relation_article)->delete())
        {
            return true;
        }
        else
        {
            return false;
        }
    }


}